<template>
  <div class="app-container">
    <!-- 查询 -->
    <el-form  :inline="true" :model="query" size="mini"  class="demo-form-inline">
      <el-form-item label="分类名称：">
        <el-input v-model="query.name" ></el-input>
      </el-form-item>
      <el-form-item label="状态：">
        <el-select clearable filterable v-model="query.status" >
          <el-option label="禁用" :value="0"></el-option>
          <el-option label="正常" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-permission="'category:search'" icon='el-icon-search' type="primary" @click="queryData">查询</el-button>
        <el-button icon='el-icon-refresh' @click="reload">重置</el-button>
        <el-button v-permission="'category:add'" icon="el-icon-circle-plus-outline" type="primary" @click="openAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table
      :data="list"
      border
      stripe
      style="width: 100%">
      <el-table-column
        align="center"
        type=index
        label="序号"
        width="50">
      </el-table-column>
      <el-table-column 
        align="center"
        prop="name"
        label="分类名称"
        width="180">
      </el-table-column>
      <el-table-column
        align="center"
        prop="sort"
        label="排序"
        width="180">
      </el-table-column>
      <el-table-column
        align="center"
        prop="remark"
        label="备注"
        width="180">
      </el-table-column>
      <el-table-column
        align="center"
        prop="status"
        label="状态"
        width="180">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">
              {{scope.row.status?'禁用':'正常'}}
          </el-tag>
        </template>                
      </el-table-column>
      <el-table-column  
        align="center"   
        label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            v-permission="'category:edit'"
            @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            v-permission="'category:detele'"
            @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页功能 -->   
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total">
    </el-pagination>
    <!-- 弹出窗口 -->
    <edit :visible="edit.visible" :formData="edit.formData" :remoteClose="remoteClose" :title="edit.title" ></edit>
  </div>
</template>

<script>
import api from '@/api/category'
import Edit from './edit.vue'
export default { 
  name:'Category',
  components: { Edit },
  data() {
    return {
      list:[],
      page:{ // 分页相关
        total:0, // 总记录数
        size:20,// 每页显示20条数据
        current:1// 当前页码
      },
      query:{},// 查询条件
      edit:{
        visible:false,
        formData:{
          id: null,
          name: '',
          sort: null,
          remark:''
        },
        title:''
      }
    }
  },
  methods: {
    // 请求数据
    fetchData(){
      api.getList(this.query,this.current,this.size).then(
        response=>{
          this.list=response.data.records
          this.page.total=response.data.total
          
        }
      )
    },
    // 编辑类别
    handleEdit(id) {
        api.getById(id).then((response)=>{
          
          if (response.code === 20000){
            this.edit.formData=response.data
            this.edit.title = '编辑'
            this.edit.visible = true
            
          }
        })
    },
    // 删除类别
    handleDelete(id) {
      this.$confirm('确认删除这条记录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          api.deleteById(id).then((response)=>{
              this.$message({
              type: response.code===20000?'success':'error',
              message: response.message
            });
          })
          this.fetchData()
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    //修改个数
    handleSizeChange(val) {       
        this.page.size=val
        this.fetchData()
    },

    // 修改当前页
    handleCurrentChange(val) {      
      this.page.current=val
      this.fetchData()
    },

    // 查询功能
    queryData() {
        this.page.current=1
        this.fetchData()
    },
    // 重载
    reload(){
      this.query={}
      this.fetchData()
    },
    // 触发关闭弹出的新增修改子组件窗口
    remoteClose(){
      this.edit.visible=false
      this.edit.formData={}
      this.fetchData()

    },
    // 弹出新增窗口
    openAdd(){
      this.edit.visible=true
      this.edit.title='新增'
      
    }
  },
  filters:{
    // 状态样式
    statusFilter(status){
      const statusMap = {0: 'danger', 1: 'success'}
      return statusMap[status]
    }
  },
  created() {
    this.fetchData()
  },
  
}
</script>

<style>

</style>